define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        moment = require('moment'),
        helper = require('js/apps/report/common');

    var ResourceView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'shown.bs.tab a.chart-tab': "onSelectTab"
        },
        initialize: function (options) {
            this.options = options;
            this.modelDct = {
                'coin': new models.CoinReport(),
                'coupon': new models.CouponReport(),
                'credit': new models.CreditReport()
            }
        },
        onSelectTab: function (e) {
            e.preventDefault();
            var ref = $(e.target).data('chart') || $(e.target).attr('href');
            $(ref).highcharts().reflow();
            return this;
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            this.options.resourceType = $('#resourceType').val();
            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            var model = this.modelDct[this.options.resourceType || 'coin'];
            this.$el.html(this.template({
                items: model.list,
                gen_dist: model.gen_dist,
                cost_dist: model.cost_dist,
                options: this.options
            }));
            helper.loadDatePicker(this.options);
            //走势曲线: 产生与消耗 || 留存 || 消费玩家
            var ccc = chart.getHighChartLineOptions(),
                bc = chart.getHighChartLineOptions(),
                cuc = chart.getHighChartLineOptions();
            _.each([ccc, ccc, bc, cuc], function (t) {
                t.series.push({
                    name: '',
                    data: []
                });
            })
            ccc.series[0].name = '产生';
            ccc.series[1].name = '消耗';
            bc.series[0].name = '余额';
            cuc.series[0].name = '消耗用户数';
            _.each(model.list, function (item) {
                ccc.xAxis.categories.push(item._id);
                bc.xAxis.categories.push(item._id);
                cuc.xAxis.categories.push(item._id);
                ccc.series[0].data.push(item.gen_amount);
                ccc.series[1].data.push(item.cost_amount);
                bc.series[0].data.push(item.balance);
                cuc.series[0].data.push(item.cost_user);
            });
            $('#create_cost').highcharts(ccc);
            $('#balance').highcharts(bc);
            $('#cost_user').highcharts(cuc);

            //产生与消耗渠道的饼图
            var createPie = chart.getHighChartPieOptions(),
                costPie = chart.getHighChartPieOptions(),
                that = this;
            _.each(model.gen_dist, function (item) {
                createPie.series[0].data.push({
                    name: item.type,
                    y: item.amount
                });
            });
            _.each(model.cost_dist, function (item) {
                costPie.series[0].data.push({
                    name: item.type,
                    y: item.amount
                });
            });
            $('#create_channel').highcharts(createPie);
            $('#cost_channel').highcharts(costPie);
            return this;
        },
        load: function () {
            var model = this.modelDct[this.options.resourceType || 'coin'],
                that = this;
            model.fetch({
                reset: true,
                data: {
                    date_range: this.options.dateRange.join(','),
                },
                success: function (c, r, o) {
                    that.renderWithData(that);
                },
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            })
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Resource.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new ResourceView({
                    dateRange: dateRange
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
